﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Village_Project.Core;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace Village_Project
{
    public partial class ServiceRecord : System.Web.UI.Page
    {
        private servicerecordLog _SA;
        string svccode = WebConfigurationManager.AppSettings["svccode1"];
        public static List<string> x = new List<string>();
        public static List<string> y = new List<string>();
        string tmp;
        DataTable table = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {





            _SA = new servicerecordLog();



            if (Session["CID"] == null)
            {
                MessageBoxurl("กรุณากดค้นหา หรือลงทะเบียนเด็กก่อน", "findchild.aspx");
            }
            else
            {

                string PSstring = Request.QueryString["PS"];
                string CIDstring = Request.QueryString["CID"];

                if (CIDstring != null && PSstring != null)
                {
                    showrecord();
                    showrecordinfo();
                    Panel3.Visible = false;
                    show_Images();
                }


                else
                {

                    show_Images();

                    if (!IsPostBack)
                    {

                        BindDatareg_educationnfo();
                        //readtodatatableeducation();
                        showrecord();
                        //String ShowValue = (string)Session["PSNumber01"];
                        //psnumber.Text = ShowValue;
                        //_SA.readtodatatable(psnumber, GridView1, GridView11, svccode);
                        Panel2.Visible = false;
                    }
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            crepsnumber();
            psnumbertosession();
            procesbutton();
            showrecord();
        }



        void showimgeiei()
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;

            using (SqlConnection conn = new SqlConnection(constr))
            {
                string sql = "SELECT SVCDesc,SVCCode FROM Service";
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
                    {
                        conn.Open();
                        ad.Fill(table);
                        conn.Close();
                    }
                }
            }
            //GridView12.DataSource = table;
            //GridView12.DataBind();

            //for (int i = 0; i < table.Rows.Count; i++)
            //{


            foreach (GridViewRow row in GridView1.Rows)
            {

                y.Clear();


                DropDownList DDL = (DropDownList)GridView1.Rows[row.RowIndex].Cells[2].FindControl("MAT");

                SqlConnection scon2 = new SqlConnection(constr);
                SqlCommand cmd2 = new SqlCommand("SELECT ACTDesc FROM Activity WHERE SVCCode=@svccode", scon2);
                //cmd2.Parameters.Add '" + table.Rows[i]["SVCCode"].ToString() + "'
                //
                cmd2.Parameters.AddWithValue("@svccode", GridView1.Rows[row.RowIndex].Cells[0].Text);

                scon2.Open();
                SqlDataReader reader = cmd2.ExecuteReader();
                while (reader.Read())
                {
                    y.Add(reader["ACTDesc"].ToString());

                }
                reader.Close();
                // scon2.Close();
                for (int c = 0; c < y.Count; c++)
                {

                    DDL.Items.Add(y[c]);
                }



                //}

            }














        }




        public void BindDatareg_educationnfo()
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);


            //string svccode1 = svccode;
            scon.Open();
            SqlCommand cmd = new SqlCommand("SELECT SVCCode,SVCDesc FROM Service", scon);
            //cmd.Parameters.AddWithValue("@svccode1", svccode);
            SqlDataReader reader = cmd.ExecuteReader();

            GridView1.DataSource = reader;
            GridView1.DataBind();
            scon.Close();

            //readtodatatableeducation();
        }





        public void readtodatatableeducation()
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);


            scon.Open();
            DataTable dtedu = new DataTable();

            SqlCommand cmd = new SqlCommand("SELECT code,Note from education where CID=@CID", scon);
            cmd.Parameters.AddWithValue("@CID", Session["CID"]);

            SqlDataReader reader8 = cmd.ExecuteReader();

            dtedu.Columns.Add("code");
            dtedu.Columns.Add("Note");
            while (reader8.Read() != false)
            {
                dtedu.Rows.Add(reader8["code"].ToString(), (reader8["Note"].ToString()));
                //x3.Add(reader8["ACTCode"].ToString());
                //x4.Add(reader8["Note"].ToString());

            }
            reader8.Close();


            for (int j = 0; j < dtedu.Rows.Count; j++)
            {
                string a = dtedu.Rows[j]["code"].ToString();
                foreach (GridViewRow GR in GridView1.Rows)
                {
                    string b = GR.Cells[0].Text.ToString();
                    CheckBox chk = (CheckBox)GR.FindControl("CaseSelectoredu");
                    TextBox tb = (TextBox)GR.FindControl("TextBoxedu");
                    if (a == b)
                    {

                        tb.Text = dtedu.Rows[j]["Note"].ToString();
                        // tb.Text=
                        chk.Checked = true;
                    }
                }
            }


            scon.Close();




        }








        public void crepsnumber()
        {

            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            SqlCommand command2 = new SqlCommand("INSERT INTO [ServiceProvision] (CID,HostID,StaffID,PSDateTime) VALUES(@CID,'1','1',@PSDateTime)", scon);
            scon.Open();
            //command2.Parameters.AddWithValue("@PSNumber", psnumber.Text);

            //command2.Parameters.AddWithValue("@HostID", Session["Host"]);
            //command2.Parameters.AddWithValue("@StaffID", Session["Staff_id"]);
            command2.Parameters.AddWithValue("@PSDateTime", DateTime.Now);
            command2.Parameters.AddWithValue("@CID", Session["CID"]);
            command2.ExecuteNonQuery();
            scon.Close();
        }












        public void procesbutton()
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);

            SelectStat.Text = string.Empty;
            bool selectorChecked3 = false;

            DataTable dt2 = new DataTable();
            dt2.Columns.Add("SVCCode");


            dt2.Columns.Add("note");
            foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox cb = (CheckBox)row.FindControl("CaseSelector3");
                if (cb != null && cb.Checked)
                {
                    selectorChecked3 = true;
                    TextBox rb = row.FindControl("TextBox3") as TextBox;
                    DataRow dr2;
                    dr2 = dt2.NewRow();
                    string SVCCode = GridView1.Rows[row.RowIndex].Cells[0].Text;
                    // string CID = GridView2.Rows[row.RowIndex].Cells[1].Text;
                    SelectStat.Text += string.Format("{0} has been added <br />", SVCCode);
                    //  string CID = GridView1.Rows[row.RowIndex].Cells[1].Text;
                    string note = rb.Text;
                    dt2.Rows.Add(SVCCode, note);

                }
            }
            SelectStat.Visible = selectorChecked3;

           


            for (int i = 0; i < dt2.Rows.Count; i++)
            {

                SqlCommand command = new SqlCommand("INSERT INTO [ServiceProvisionDetails] (ACTCode,PSNumber,Note) VALUES('" + dt2.Rows[i]["SVCCode"].ToString() + "',@PSNumber,'" + dt2.Rows[i]["note"].ToString() + "')", scon);
                scon.Open();
                command.Parameters.AddWithValue("@PSNumber", Session["PSNumber"]);
                command.ExecuteNonQuery();
                scon.Close();

            }

          
        }



        public void psnumbertosession()
        {

            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            SqlCommand CmdSql47 = new SqlCommand("SELECT PSNumber FROM [ServiceProvision] WHERE PSNumber = (SELECT MAX(PSNumber)  FROM [ServiceProvision])", scon);
            scon.Open();

            SqlDataReader reader = CmdSql47.ExecuteReader();
            // DataTable dt = new DataTable();
            //  dt.Load(reader);\
            // = reader;

            if (reader.Read())
            {
                Session["PSNumber"] = reader["PSNumber"].ToString();



            }
            else
            {

            }
            scon.Close();
            reader.Close();



        }





        protected void showrecord()
        {


            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            //ต้องการแก้เคสที่มีอยู่แล้ว
            scon.Open();
            SqlCommand cmd = new SqlCommand("SELECT [PSNumber] as PS,[HostID],[StaffID],[CID],[PSDateTime] from ServiceProvision where CID=@CID", scon);
            cmd.Parameters.AddWithValue("@CID", Session["CID"]);

            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            showrec.DataSource = ds;
            showrec.DataBind();
            scon.Close();




        }


        protected void familylist_SelectedIndexChanged(object sender, GridViewPageEventArgs e)
        {
            showrec.PageIndex = e.NewPageIndex;
            showrecord();
        }







        protected void showrecordinfo()
        {

            string CIDstring = Request.QueryString["CID"];
            string PSstring = Request.QueryString["PS"];

            if (CIDstring != null && PSstring != null)
            {
                string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
                SqlConnection scon = new SqlConnection(constr);
                //ต้องการแก้เคสที่มีอยู่แล้ว
                scon.Open();
                SqlCommand cmd = new SqlCommand("SELECT a.[CID],d.SVCDesc as dec,b.Note as Note FROM [ServiceProvision] a join [ServiceProvisionDetails] b on a.PSNumber=b.PSNumber join [Service] d on b.ACTCode=d.SVCCode where a.CID=@CID  and b.PSNumber=@PSNumber", scon);
                cmd.Parameters.AddWithValue("@CID", CIDstring);
                cmd.Parameters.AddWithValue("@PSNumber", PSstring);

                DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                showrecinfo.DataSource = ds;
                showrecinfo.DataBind();
                scon.Close();

            }

            else
            {

            }


        }


        public void show_Images()
        {
            string sj = "";



            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);

            SqlCommand CmdSql2 = new SqlCommand("SELECT Child_Pic FROM [Person] where CID=@CID", conn);
            conn.Open();
            CmdSql2.Parameters.AddWithValue("@CID", Session["CID"]);

            SqlDataReader reader2 = CmdSql2.ExecuteReader();

            if (reader2.Read())
            {
                sj = reader2["Child_Pic"].ToString();
            }
            else
            {
                sj = "~/pic/profile/nopic.jpg";
            }
            conn.Close();
            reader2.Close();


            Image1.ImageUrl = (sj);
        }



        private void MessageBoxurl(string msg, string urld)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "');" + "window.location.href='" + urld + "';" + "</script>";
            Page.Controls.Add(lbl);
        }

        protected void back_Click(object sender, EventArgs e)
        {
            Response.Redirect("ServiceRecord.aspx");
        }
    }
}